import { defHttp } from '@/utils/http/axios';

export interface ProcessMeta {
  id?: number;
  name: string;
  key: string;
  status?: string;
  // ...其他字段
}

export function getProcessList(params: any) {
  return defHttp.get({ url: '/workflow/processMeta/list', params });
}

export function getProcessDetail(id: number) {
  return defHttp.get({ url: `/workflow/processMeta/${id}` });
}

export function addProcess(data: ProcessMeta) {
  return defHttp.post({ url: '/workflow/processMeta', data });
}

export function editProcess(data: ProcessMeta) {
  return defHttp.put({ url: '/workflow/processMeta', data });
}

export function deleteProcesses(idList: number[]) {
  return defHttp.delete({ url: `/workflow/processMeta/batch/${idList.join(',')}` });
}

// 流程部署、实例、任务相关API
export function deployProcess(file: File) {
  const formData = new FormData();
  formData.append('file', file);
  return defHttp.post({ url: '/workflow/process/deploy', data: formData });
}

export function startProcess(data: any) {
  return defHttp.post({ 
    url: '/workflow/processFlow/startProcess', 
    data 
  }, {
    isTransformResponse: false // 返回原始响应数据
  });
}

export function getTasks(params: any) {
  return defHttp.get({ url: '/workflow/process/tasks', params });
}

export function completeTask(data: any) {
  return defHttp.post({ url: '/workflow/process/complete', data });
}

export function getProcessHistory(processInstanceId: string) {
  return defHttp.get({ url: '/workflow/process/history', params: { processInstanceId } });
}

// 新增API接口

// 获取已授权的流程列表
export function getAuthorizedProcessList(params?: any) {
  return defHttp.get({ url: '/workflow/processFlow/authorizedProcessList', params });
}

// 获取流程表单绑定信息
export function getProcessFormBind(params: any) {
  return defHttp.get({ url: '/workflow/processFlow/processFormBind', params });
}

// 获取流程版本信息
export function getProcessVersion(processId: string) {
  return defHttp.get({ url: `/workflow/processFlow/processVersion/${processId}` });
}

// 通过流程版本ID获取流程版本信息
export function getProcessVersionById(versionId: number) {
  return defHttp.get({ url: `/workflow/processFlow/processVersionById/${versionId}` });
}

// 获取流程版本信息（通过processKey）
export function getProcessVersionByKey(processKey: string) {
  return defHttp.get({ url: `/workflow/processFlow/processVersionByKey/${processKey}` });
}

// 流程实例相关API
export function getProcessInstanceList(params: any) {
  return defHttp.get({ 
    url: '/workflow/processFlow/processInstanceList', 
    params 
  }, {
    isTransformResponse: false // 返回原始响应数据
  });
}

export function getProcessInstanceDetail(instanceId: string) {
  return defHttp.get({ url: `/workflow/processFlow/processInstanceDetail/${instanceId}` });
}

export function cancelProcessInstance(instanceId: string, reason?: string) {
  return defHttp.post({ url: `/workflow/processFlow/cancelProcessInstance/${instanceId}`, data: { reason } });
}

// 任务相关API
export function getMyTasks(params: any) {
  return defHttp.get({ url: '/workflow/processFlow/myTasks', params });
}

export function getTaskDetail(taskId: string) {
  return defHttp.get({ url: `/workflow/processFlow/taskDetail/${taskId}` });
}

export function claimTask(taskId: string) {
  return defHttp.post({ url: `/workflow/processFlow/claimTask/${taskId}` });
}

export function unclaimTask(taskId: string) {
  return defHttp.post({ url: `/workflow/processFlow/unclaimTask/${taskId}` });
}

export function delegateTask(taskId: string, userId: string) {
  return defHttp.post({ url: `/workflow/processFlow/delegateTask/${taskId}`, data: { assignee: userId } });
}

export function transferTask(taskId: string, userId: string) {
  return defHttp.post({ url: `/workflow/processFlow/transferTask/${taskId}`, data: { assignee: userId } });
}

export function getApprovalHistory(processInstanceId: string) {
  return defHttp.get({ url: `/workflow/processFlow/approvalHistory/${processInstanceId}` });
}

// 获取节点状态
export function getNodeStatus(processInstanceId: string) {
  return defHttp.get({ url: `/workflow/processFlow/nodeStatus/${processInstanceId}` });
}

// 获取下一步节点信息
export function getNextNodes(data: any) {
  return defHttp.post({ url: '/workflow/processFlow/getNextNodes', data });
}

// 获取当前任务节点配置
export function getCurrentTaskNodeConfig(taskId: string) {
  return defHttp.get({ url: `/workflow/processFlow/currentTaskNodeConfig/${taskId}` });
}

// 添加审批记录
export function addApprovalRecord(data: any) {
  return defHttp.post({ url: '/workflow/processFlow/addApprovalRecord', data });
}

// 提交审核
export function submitApproval(data: any) {
  return defHttp.post({ 
    url: '/workflow/processFlow/submitApproval', 
    data 
  }, {
    isTransformResponse: false // 返回原始响应数据
  });
}

export function getWorkbenchData() {
  return defHttp.get({ url: '/workflow/processFlow/workbenchData' });
}

export function getWorkbenchTasks(params: any) {
  return defHttp.get({ url: '/workflow/processFlow/workbenchTasks', params });
}

export function getProcessHistoryList(params: any) {
  return defHttp.get({ url: '/workflow/processFlow/processHistoryList', params });
}

export function getTaskHistoryList(params: any) {
  return defHttp.get({ url: '/workflow/processFlow/taskHistoryList', params });
}

export function exportHistoryData(params: any) {
  return defHttp.post({ url: '/workflow/processFlow/exportHistoryData', data: params });
}

// 获取人员列表
export function getPersonnelList(data: any) {
  return defHttp.post({ url: '/workflow/processFlow/getPersonnelList', data });
}
